/**
 * Project Name:kany-project-spring-boot
 * File Name:ContactController.java
 * Package Name:me.kany.project.spring.boot.controller
 * Date:2016年5月27日下午2:43:16
 * Copyright (c) 2016, Jason.Wang All Rights Reserved.
 *
*/

package me.kany.project.spring.boot.controller;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import me.kany.utils.excel.poi.ReadExcel;

/**
 * ClassName:ContactController <br/>
 * Function: 创建联系人的文件类型. <br/>
 * Date: 2016年5月27日 下午2:43:16 <br/>
 * 
 * @author Jason.Wang
 * @version
 * @since JDK 1.7
 * @see
 */
@RestController
@RequestMapping("/contact")
public class ContactController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 
	 * execute:默认的调用方法. <br/>
	 *
	 * @author Jason.Wang
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping
	public ModelAndView execute() {
		ModelAndView modelAndView = new ModelAndView("front/contact");
		logger.info("请求了通信录的主要方法...");
		return modelAndView;
	}

	/**
	 * 
	 * upload:从前端上传Excel文件，使用POI解析完成之后返回给前端JSONObject. <br/>
	 *
	 * @author Jason.Wang
	 * @param file
	 * @return
	 * @since JDK 1.7
	 */
	@ResponseBody
	@RequestMapping(value = "upload", method = RequestMethod.POST)
	public Object upload(@RequestParam(value = "file", required = false) MultipartFile file) {
		JSONObject jsonObject = new JSONObject();
		ReadExcel readExcel = new ReadExcel();

		logger.info("调用上传文件的方式");
		if (null != file && (file.getOriginalFilename().lastIndexOf(".xls") > -1 || file.getOriginalFilename().lastIndexOf(".xlsx") > -1)) {
			try {
				return JSONObject.toJSON(readExcel.readExcel(file.getInputStream()));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return jsonObject;
	}
}
